package com.jswdwsx.common.utils;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

public class ResponseUtils {

    private static final int SUCCESS_CODE = 0;
    private static final String SUCCESS_MSG = "success";

    /**
     * 非http响应码，而是http状态码200时请求体中自定义的响应码
     */
    private static final int READABLE_FAIL_CODE = 1;
    private static final int UNREADABLE_FAIL_CODE = 2;
    private static final int NOT_FOUND = 404;
    private static final int UNAUTHORIZED = 401;

    public static ResponseEntity success() {
        return ResponseUtils.success(null);
    }

    public static ResponseEntity success(Object data) {
        return ResponseUtils.success(ResponseUtils.SUCCESS_MSG, data);
    }

    public static ResponseEntity success(String message, Object data) {
        return ResponseUtils.response(ResponseUtils.SUCCESS_CODE, message, data);
    }

    public static ResponseEntity fail(String msg) {
        return ResponseUtils.fail(ResponseUtils.READABLE_FAIL_CODE, msg);
    }

    public static ResponseEntity systemFail(String msg) {
        return ResponseUtils.fail(ResponseUtils.UNREADABLE_FAIL_CODE, msg);
    }

    public static ResponseEntity fail(Integer code, String msg) {
        return ResponseUtils.response(code, msg, null);
    }

    public static ResponseEntity validateFail(String msg) {
        return ResponseUtils.response(ResponseUtils.READABLE_FAIL_CODE, msg, null);
    }

    public static ResponseEntity notFound() {
        return ResponseUtils.response(ResponseUtils.NOT_FOUND, "对应资源不存在", null);
    }

    private static ResponseEntity response(Integer code, String message, Object data) {
        return new ResponseEntity<>(new ResponseBody(code, message, data), HttpStatus.OK);
    }

    public static ResponseEntity unauthorized() {
        ResponseBody responseBody = new ResponseBody(ResponseUtils.UNAUTHORIZED, "登录失败", null);
        return new ResponseEntity<>(responseBody, HttpStatus.UNAUTHORIZED);
    }
}
